﻿// Author:					Joe Audette
// Created:					2011-03-21
// Last Modified:			2011-03-22
// 
// The use and distribution terms for this software are covered by the 
// Common Public License 1.0 (http://opensource.org/licenses/cpl.php)  
// which can be found in the file CPL.TXT at the root of this distribution.
// By using this software in any fashion, you are agreeing to be bound by 
// the terms of this license.
//
// You must not remove this notice, or any other, from this software.

using System;
using System.Collections;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Globalization;
using System.IO;
using System.Text;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using mojoPortal.Web;
using mojoPortal.Web.Framework;
using mojoPortal.Web.UI;
using log4net;
using mojoPortal.Business;
using mojoPortal.Business.WebHelpers;
using Resources;



namespace mojoPortal.Web.AdminUI
{

    public partial class CssEditorPage : NonCmsBasePage
    {
        protected string skinName = string.Empty;
        protected string cssFile = string.Empty;
        private string skinBasePath = string.Empty;
        private bool allowEditing = false;

        protected void Page_Load(object sender, EventArgs e)
        {

            if ((!WebUser.IsInRoles(siteSettings.RolesThatCanManageSkins)))
            {
                SiteUtils.RedirectToAccessDeniedPage(this);
                return;
            }

            LoadSettings();

            if (!allowEditing)
            {
                WebUtils.SetupRedirect(this, SiteRoot + "/DesignTools/SkinList.aspx");
                return;
            }

            if (!Directory.Exists(Server.MapPath(skinBasePath + skinName)))
            {   //skin doesn't exist go back to skin list
                WebUtils.SetupRedirect(this, SiteRoot + "/DesignTools/SkinList.aspx");
                return;
            }

            if (!File.Exists(Server.MapPath(skinBasePath + skinName + "/" + cssFile)))
            {   //css file doesn't exist go back to skin manager
                WebUtils.SetupRedirect(this, SiteRoot + "/DesignTools/ManageSkin.aspx?s=" + skinName);
                return;
            }

            PopulateLabels();
            PopulateControls();

        }

        private void PopulateControls()
        {
            if (!IsPostBack) 
            { 
                BindCss(); 
            }
            
        }

        private void BindCss()
        {
            txtCss.Text = File.ReadAllText(Server.MapPath(skinBasePath + skinName + "/" + cssFile), Encoding.UTF8); //should this be ascii?


        }

        void btnSave_Click(object sender, EventArgs e)
        {
            using (StreamWriter writer = File.CreateText(Server.MapPath(skinBasePath + skinName + "/" + cssFile)))
            {
                writer.Write(txtCss.Text);
            }

            WebUtils.SetupRedirect(this, Request.RawUrl);
        }


        private void PopulateLabels()
        {
            string title = string.Format(CultureInfo.InvariantCulture, Resource.ManageSkinFormat, skinName);
            Title = SiteUtils.FormatPageTitle(siteSettings, title);

            lnkAdminMenu.Text = Resource.AdminMenuLink;
            lnkAdminMenu.NavigateUrl = SiteRoot + "/Admin/AdminMenu.aspx";

            lnkAdvancedTools.Text = Resource.AdvancedToolsLink;
            lnkAdvancedTools.NavigateUrl = SiteRoot + "/Admin/AdvancedTools.aspx";

            lnkDesignerTools.Text = DevTools.DesignTools;
            lnkDesignerTools.NavigateUrl = SiteRoot + "/DesignTools/Default.aspx";

            lnkSkinList.Text = DevTools.SkinManagement;
            lnkSkinList.NavigateUrl = SiteRoot + "/DesignTools/SkinList.aspx";

            lnkSkin.Text = skinName;
            lnkSkin.NavigateUrl = SiteRoot + "/DesignTools/ManageSkin.aspx?s=" + skinName;

            lnkThisPage.Text = cssFile;
            lnkThisPage.NavigateUrl = Request.RawUrl;

            btnSave.Text = Resource.SaveButton;

        }

        private void LoadSettings()
        {
            skinBasePath = "~/Data/Sites/" + siteSettings.SiteId.ToInvariantString() + "/skins/";
            skinName = WebUtils.ParseStringFromQueryString("s", string.Empty);
            cssFile = WebUtils.ParseStringFromQueryString("f", string.Empty);
            allowEditing = WebConfigSettings.AllowEditingSkins && (WebConfigSettings.AllowEditingSkinsInChildSites || siteSettings.IsServerAdminSite);

        }

       


        #region OnInit

        override protected void OnInit(EventArgs e)
        {
            base.OnInit(e);
            this.Load += new EventHandler(this.Page_Load);
            btnSave.Click += new EventHandler(btnSave_Click);

            SuppressMenuSelection();
            SuppressPageMenu();


        }

        

        #endregion
    }
}
